﻿Imports System.Data
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid

Public Class Thu1
    Private state As Boolean 'true là tạo mới
    Private Sub setReadonly(ByVal istrue As Boolean)
        Me.MaTextEdit.Properties.ReadOnly = istrue
        Me.SochungtuTextEdit.Properties.ReadOnly = istrue
        Me.DonviTextEdit.Properties.ReadOnly = istrue
        Me.NguoinopTextEdit.Properties.ReadOnly = istrue
        Me.NgayDateEdit.Properties.ReadOnly = istrue
        If istrue Then
            Me.IGridView1.OptionsBehavior.Editable = False
            Me.IGridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None
        Else
            Me.IGridView1.OptionsBehavior.Editable = True
            Me.IGridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom
        End If

    End Sub
    Private Sub BarButtonItemThoat_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        Try
            Me.setReadonly(False)
            Me.MaTextEdit.Focus()
            Me.ThuBindingSource.AddNew()
            Me.state = True
            Me.BarButtonItemLuu.Enabled = True
            Me.BarButtonItemXoa.Enabled = True
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Frm_DonMau_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PcvgppDataSet.khachhang' table. You can move, or remove it, as needed.
        Me.KhachhangTableAdapter.Fill(Me.PcvgppDataSet.khachhang)
        Me.ThuTableAdapter.Fill(Me.PcvgppDataSet.thu)
        Me.ThuchitietTableAdapter.Fill(Me.PcvgppDataSet.thuchitiet)
        setReadonly(True)
        Me.BarButtonItemLuu.Enabled = False
        Me.BarButtonItemXoa.Enabled = False
        Me.IGridView1.BestFitColumns()
        'Me.IGridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
        Me.IGridView1.OptionsNavigation.AutoFocusNewRow = True

    End Sub

    Private Sub BarButtonItemLuu_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridView1.CloseEditor()
            Me.ThuBindingSource.EndEdit()
            Me.ThuchitietBindingSource.EndEdit()
            If Me.PcvgppDataSet.HasChanges Then
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Me.ThuTableAdapter.Update(Me.PcvgppDataSet.thu)
                    Me.ThuchitietTableAdapter.Update(Me.PcvgppDataSet.thuchitiet)
                    Me.state = False
                    Me.setReadonly(True)
                    Me.BarButtonItemLuu.Enabled = False
                    Me.BarButtonItemXoa.Enabled = False
                End If
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Function checkExisted()
        For index = 0 To Me.IGridView1.RowCount - 1
            If Me.IGridView1.GetRowCellValue(index, "ma") = Me.MaTextEdit.EditValue Then
                Return True
                Exit Function
            End If
        Next
        Return False
    End Function

    Private Sub MaTextEdit_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)
        If checkExisted() And state Then
            Me.MaTextEdit.ErrorText = "Mã đã tồn tại"
            e.Cancel = True

        End If
    End Sub

    Private Sub BarButtonItemSua_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.BarButtonItemLuu.Enabled = True
        Me.BarButtonItemXoa.Enabled = True
        Me.setReadonly(False)
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.ThuBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.ThuBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItem6_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        Me.ThuBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItem7_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        Me.ThuBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Xóa' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.ThuBindingSource.RemoveCurrent()
            Me.ThuTableAdapter.Update(Me.PcvgppDataSet.thu)
            Me.state = False
            Me.setReadonly(True)
            Me.BarButtonItemLuu.Enabled = False
            Me.BarButtonItemXoa.Enabled = False
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.ThuBindingSource.EndEdit()
        If Me.PcvgppDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PcvgppDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

    Private Sub DonviTextEdit_GotFocus(sender As Object, e As System.EventArgs) Handles DonviTextEdit.GotFocus
        Me.DonviTextEdit.ShowPopup()
    End Sub

    Private Sub DonviTextEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DonviTextEdit.KeyDown
        If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then

            IGridView1.AddNewRow()
            Me.IGridView1.SetFocusedValue(Me.MaTextEdit.EditValue)
            IGridView1.FocusedRowHandle = 4
            IGridView1.FocusedColumn = IGridView1.VisibleColumns(0)
            IGridView1.ShowEditor()


        End If
    End Sub

    Private Sub IGridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridView1.CellValueChanged
        Me.IGridView1.UpdateSummary()
        Me.TongtienTextEdit.EditValue = Me.IGridView1.Columns("phatsinh").SummaryItem.SummaryValue
    End Sub

    Private Sub IGridView1_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridView1.FocusedRowChanged
        Dim currentView As GridView = CType(sender, GridView)
        ' The group row's handle. 
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return

        ' Get group summary values. 
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Text = ""
        ' Iterate through group summaries. 
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            ' Get the summry item's Tag property and summary value. 
            Text += sumItem.Tag.ToString() + ": " + val.ToString() + "\t"
            Me.TaikhoannoTextEdit.EditValue = Text
        Next
    End Sub

    Private Sub IGridView1_InvalidRowException(sender As Object, e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) Handles IGridView1.InvalidRowException
        e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction
    End Sub

    Private Sub IGridView1_ValidateRow(sender As Object, e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) Handles IGridView1.ValidateRow
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row
        Dim view As GridView = CType(sender, GridView)
        Try
            'If DirectCast(row("ma"), String) = String.Empty OrElse DirectCast(row("taikhoanco"), String) = String.Empty OrElse DirectCast(row("chungtuthu"), String) = String.Empty OrElse DirectCast(row("phatsinh"), String) = String.Empty Then
            '    e.Valid = False
            '    e.ErrorText = "Error"
            'End If
            Dim taikhoanco As GridColumn = view.Columns("taikhoanco")
            Dim tkco As String = view.GetRowCellValue(e.RowHandle, taikhoanco)
            MessageBox.Show(tkco)
            If tkco = String.Empty Then
                e.Valid = False
                'e.ErrorText = "Error"
                view.SetColumnError(taikhoanco, "tài khoản nợ không đc rỗng !")
            End If
        Catch er As Exception
            e.Valid = False
            'e.ErrorText = "Please fill the 'Trademark and the 'Model' fields"
        End Try
    End Sub

    Private Sub IGridView1_ValidatingEditor(sender As Object, e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridView1.ValidatingEditor
        Dim View As GridView = sender
        If View.FocusedColumn.FieldName = "phatsinh" Then
            'Get the currently edited value 
            Dim phatsinh As Double = Convert.ToDouble(e.Value)
            'Specify validation criteria 
            If phatsinh = 0 Then
                e.Valid = False
                e.ErrorText = "Số lượng phải lớn hơn 0"
            End If
            'If discount > 0.2 Then
            '    e.Valid = False
            '    e.ErrorText = "Reduce the amount (20% is maximum)"
            'End If
        End If
    End Sub


End Class